package com.example.demo.framework.util;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class RestTemplateUtil {

	private RestTemplate restTemplate;

	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}

	public String post(ServletRequest req, String url, Map<String, ?> params) {
		ResponseEntity<String> rss = request(req, url, HttpMethod.POST, params);
		return rss.getBody();
	}

	public String get(ServletRequest req, String url, Map<String, ?> params) {
		ResponseEntity<String> rss = request(req, url, HttpMethod.GET, params);
		return rss.getBody();
	}

	public String delete(ServletRequest req, String url, Map<String, ?> params) {
		ResponseEntity<String> rss = request(req, url, HttpMethod.DELETE, params);
		return rss.getBody();
	}

	public String put(ServletRequest req, String url, Map<String, ?> params) {
		ResponseEntity<String> rss = request(req, url, HttpMethod.PUT, params);
		return rss.getBody();
	}

	/**
	 * @param req
	 * @param url
	 * @param method
	 * @param params
	 *            maybe null
	 * @return
	 */
	private ResponseEntity<String> request(ServletRequest req, String url, HttpMethod method, Map<String, ?> params) {
		HttpServletRequest request = (HttpServletRequest) req;
		// 获取header信息
		HttpHeaders requestHeaders = new HttpHeaders();
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String key = (String) headerNames.nextElement();
			String value = request.getHeader(key);
			requestHeaders.add(key, value);
		}
		// 获取parameter信息
		if (params == null) {
			params = request.getParameterMap();
		}

		HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
		ResponseEntity<String> rss = restTemplate.exchange(url, method, requestEntity, String.class, params);
		return rss;
	}
}
